package com.hq.sys.controller;

import com.hq.sys.eneity.UserVo;
import com.hq.sys.service.UserService;
import com.hq.sys.util.DataGridView;
import com.hq.sys.util.ResultObj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @BelongsPackage: com.hq.sys.controller
 * @Author: jiangjunjie
 * @CreateTime: 2024-12-23  14:31
 * @Description: TODO
 * @Version: 1.8
 */
@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;
    //查看所有用户
    @RequestMapping("loadAllUser")
    public DataGridView loadAllUser(UserVo userVo){
        return userService.queryAllUser(userVo);
    }
    //添加用户
    @RequestMapping("addUser")
    public ResultObj addUser(UserVo userVo){
        try {
            userService.addUser(userVo);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }
    //修改用户
    @RequestMapping("updateUser")
    public ResultObj updateUser(UserVo userVo){
        try {
            userService.updateUser(userVo);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }
    //删除用户
    @RequestMapping("deleteUser")
    public ResultObj deleteUser(Integer userid){
        try {
            userService.deleteUser(userid);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }
    //批量删除用户
    @RequestMapping("deleteBatchUser")
    public ResultObj deleteBatchUser(Integer[] ids){
        try {
            userService.deleteBatchUser(ids);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }
    //重置密码
    @RequestMapping("resetUserPwd")
    public ResultObj resetUserPwd(UserVo userVo){
        try {
            userService.resetUserPwd(userVo);
            return ResultObj.RESET_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.RESET_ERROR;
        }
    }
    //分配角色
    @RequestMapping("saveUserRole")
    public ResultObj saveUserRole(UserVo userVo){
        try {
            userService.saveUserRole(userVo);
            return ResultObj.DISPATCH_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DISPATCH_ERROR;
        }
    }
    //分配角色
    @RequestMapping("initUserRole")
    public DataGridView initUserRole(Integer userid){
        return userService.initUserRole(userid);
    }


}
